﻿Imports System.IO
Public Class UMDHelper

    Dim mUpMetData As UpMetData
    Public Sub New(ByVal data As UpMetData)

        mUpMetData = data

    End Sub


    'Public Sub ExportFSLData(ByVal mUpMetData As UpMetData, ByVal mPath As String)
    'Public Sub Export(ByVal fname As String)
    Public Sub ExportFSLData(ByVal filename As String)
        Dim m_filename As String
        m_filename = filename
        Dim mDataTime As String = ""
        'mDataTime = "    254" & mUpMetData
        Dim mMoonth() As String = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"}
        Dim Lat As Double = mUpMetData.title.Latitude.Remove(mUpMetData.title.Latitude.Length - 1, 1) '取得纬度
        Dim Lon As Double = mUpMetData.title.Longitude.Remove(mUpMetData.title.Longitude.Length - 1, 1) '取得经度
        Dim mSunStr As String = ""
        Try
            For i As Integer = 0 To mUpMetData.reconnaissance.ArrReconnaissance.Length - 1

                ' mDataTime = "    254" & (mUpMetData.reconnaissance.ArrReconnaissance(i).Time - mUpMetData.title.TimeZone).ToString.PadLeft(7) & mUpMetData.reconnaissance.ArrReconnaissance(i).Day.PadLeft(7).ToString
                mDataTime = "    254" & (mUpMetData.reconnaissance.ArrReconnaissance(i).Time).ToString.PadLeft(7) & mUpMetData.reconnaissance.ArrReconnaissance(i).Day.PadLeft(7).ToString
                Dim mMoon As String = ""
                Select Case mUpMetData.reconnaissance.ArrReconnaissance(i).Month.Trim
                    Case "01"
                        mMoon = mMoonth(0)
                    Case "02"
                        mMoon = mMoonth(1)
                    Case "03"
                        mMoon = mMoonth(2)
                    Case "04"
                        mMoon = mMoonth(3)
                    Case "05"
                        mMoon = mMoonth(4)
                    Case "06"
                        mMoon = mMoonth(5)
                    Case "07"
                        mMoon = mMoonth(6)
                    Case "08"
                        mMoon = mMoonth(7)
                    Case "09"
                        mMoon = mMoonth(8)
                    Case "10"
                        mMoon = mMoonth(9)
                    Case "11"
                        mMoon = mMoonth(10)
                    Case "12"
                        mMoon = mMoonth(11)
                End Select
                mDataTime = mDataTime & mMoon.PadLeft(9).ToString
                mDataTime = mDataTime & mUpMetData.reconnaissance.ArrReconnaissance(i).Year.PadLeft(8).ToString & vbCrLf
                'Dim mLOLA As String = "      1" & "  9" & mUpMetData.title.WeatherID.Substring(2, 2) & mUpMetData.title.WeatherID.Substring(6, 2) & mUpMetData.title.WeatherID.Remove(1, 3).PadLeft(7) & (mUpMetData.title.Latitude.ToString).PadLeft(8) & (mUpMetData.title.Longitude.ToString).PadLeft(7) _
                '                        & mUpMetData.title.TerraHeight.ToString.PadLeft(6) & "  99999" & vbCrLf

                Dim id As String = ""
                If mUpMetData.title.WeatherID.Length <> 8 Then
                    id = mUpMetData.title.WeatherID
                Else
                    id = mUpMetData.title.WeatherID.Substring(3, 1) + mUpMetData.title.WeatherID.Substring(6, 2) + "99"
                End If


                Dim mLOLA As String = "      1" & id.PadLeft(7) + "  99999" & (mUpMetData.title.Latitude.ToString).PadLeft(8) & (mUpMetData.title.Longitude.ToString).PadLeft(7) _
                                      & mUpMetData.title.TerraHeight.ToString.PadLeft(6) & "   2303" & vbCrLf


                Dim mLays As String = "      2    500    500    500" & (mUpMetData.reconnaissance.ArrReconnaissance(i).TierCount + 4).ToString.PadLeft(7) & "  99999      3" & vbCrLf
                Dim mStation As String = "      3          HAHA                99999     ms" & vbCrLf ' _
                ' & "      9  99999  99999  99999  99999  99999  99999" & vbCrLf

                Dim mTop As String = mDataTime & mLOLA & mLays & mStation
                mSunStr = mSunStr & mTop
                Dim Str As String = ""
                For j As Integer = 0 To mUpMetData.reconnaissance.ArrReconnaissance(i).ArrtierData.Length - 1
                    If j = 0 Then
                        Str = Str & "      9" & (mUpMetData.reconnaissance.ArrReconnaissance(i).ArrtierData(j).Pa / 10).ToString.PadLeft(7)
                    Else

                        Str = Str & "      4" & (mUpMetData.reconnaissance.ArrReconnaissance(i).ArrtierData(j).Pa / 10).ToString.PadLeft(7)
                    End If

                    If mUpMetData.reconnaissance.ArrReconnaissance(i).ArrtierData(j).DistanceTerraHeight = -99999 Then
                        Str = Str & "99999".PadLeft(7)
                    Else
                        Str = Str & (mUpMetData.reconnaissance.ArrReconnaissance(i).ArrtierData(j).DistanceTerraHeight + mUpMetData.title.TerraHeight).ToString.PadLeft(7)
                    End If
                    If mUpMetData.reconnaissance.ArrReconnaissance(i).ArrtierData(j).DryTemperature = -9990 Then
                        Str = Str & "99999".PadLeft(7)
                    Else
                        Str = Str & mUpMetData.reconnaissance.ArrReconnaissance(i).ArrtierData(j).DryTemperature.PadLeft(7)
                    End If
                    If mUpMetData.reconnaissance.ArrReconnaissance(i).ArrtierData(j).DewPointTemperature = -9990 Then
                        Str = Str & "99999".PadLeft(7)
                    Else
                        Str = Str & mUpMetData.reconnaissance.ArrReconnaissance(i).ArrtierData(j).DewPointTemperature.PadLeft(7)
                    End If
                    If mUpMetData.reconnaissance.ArrReconnaissance(i).ArrtierData(j).WindVane = 999 Then
                        Str = Str & "99999".PadLeft(7)
                    Else
                        Str = Str & mUpMetData.reconnaissance.ArrReconnaissance(i).ArrtierData(j).WindVane.PadLeft(7)
                    End If
                    If mUpMetData.reconnaissance.ArrReconnaissance(i).ArrtierData(j).WindSpeed = 9990 Then
                        Str = Str & "99999".PadLeft(7)
                    Else
                        Str = Str & mUpMetData.reconnaissance.ArrReconnaissance(i).ArrtierData(j).WindSpeed.PadLeft(7)
                    End If
                    Str = Str & vbCrLf
                Next
                mSunStr = mSunStr & Str
            Next
            FileOpen(1, m_filename, OpenMode.Output)
            PrintLine(1, mSunStr)
            FileClose(1)
        Catch ex As Exception
            MsgBox("写文件出错,请改名称重试", MsgBoxStyle.OkOnly, "提示")
        End Try



    End Sub
    ''' <summary>
    ''' 导出TD6201气象数据
    ''' </summary>
    ''' <param name="FileName"></param>
    ''' <remarks></remarks>
    Public Sub ExportTD6201Data(ByVal FileName As String)



        Dim pa As String = ""
        Dim pa1 As String = ""

        Dim DistanceTerraHeight As String = ""
        Dim DistanceTerraHeight1 As String = ""

        Dim DryTemperature As String = ""
        Dim DryTemperature1 As String = ""

        'Dim DewPointTemperature As String = ""
        'Dim DewPointTemperature1 As String = ""

        Dim WindVane As String = ""
        Dim WindVane1 As String = ""

        Dim WindSpeed As String = ""
        Dim intwindspeed As Integer
        Dim WindSpeed1 As String = ""

        Dim sngRH As String

        Dim strIndex As Integer = -1
        Dim str As String = ""
        Dim rdate As String = ""


        '小阳新加入代码
        'mUpMetData.title.WeatherID = 51888



        Dim TIME_SINCE_RELEASE As String = "9999" '小阳该"9999" 为"09999"
        'Dim str As String = ""
        Dim myear As String = ""
        Dim swr As StreamWriter
        swr = New StreamWriter(FileName)
        Dim Lat As String = mUpMetData.title.Latitude.Remove(mUpMetData.title.Latitude.Length - 1, 1) '取得经度
        Dim Lon As String = mUpMetData.title.Longitude.Remove(mUpMetData.title.Longitude.Length - 1, 1) '取得纬度
        For f1 As Integer = 0 To mUpMetData.reconnaissance.ArrReconnaissance.Length - 1


            If mUpMetData.reconnaissance.ArrReconnaissance(f1).TierCount > 0 Then
                If mUpMetData.reconnaissance.ArrReconnaissance(f1).Year < 50 Then
                    myear = mUpMetData.reconnaissance.ArrReconnaissance(f1).Year '"20" &
                Else
                    myear = mUpMetData.reconnaissance.ArrReconnaissance(f1).Year '"19"&
                End If

                '经纬度变换
                Lat = Me.CharIndex(Lat, 4)
                ' Lat = Lat.ToString.PadLeft(4, "0")
                Lon = Me.CharIndex(Lon, 5)
                ' Lon = Lon.ToString.PadLeft(5, "0")
                '年
                myear = myear & mUpMetData.reconnaissance.ArrReconnaissance(f1).Month & mUpMetData.reconnaissance.ArrReconnaissance(f1).Day & mUpMetData.reconnaissance.ArrReconnaissance(f1).Time
                '写入每一时次的控制信息
                str = mUpMetData.title.WeatherID.PadLeft(8, "0") & Lat.PadLeft(4, "0") & mUpMetData.title.Latitude.Remove(0, mUpMetData.title.Latitude.Length - 1) _
                & Lon.PadLeft(5, "0") & mUpMetData.title.Longitude.Remove(0, mUpMetData.title.Longitude.Length - 1) & myear & mUpMetData.reconnaissance.ArrReconnaissance(f1).TierCount.ToString.PadLeft(3, "0") '& "0" '& "9"
                swr.Write(str) '写入每一时次的控制信息,不换行
                For f2 As Integer = 0 To mUpMetData.reconnaissance.ArrReconnaissance(f1).ArrtierData.Length - 1


                    '气压，单位十帕，5位
                    If mUpMetData.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).Pa.ToString = 0 Then
                        '+1
                        pa1 = 99999
                        'pa = pa1.ToString.PadLeft(5)
                    Else
                        pa = mUpMetData.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).Pa.ToString.PadLeft(5, "0")
                    End If
                    '处理每层位势(海拔)高度，单位m，缺失写入-99999，6位
                    If mUpMetData.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DistanceTerraHeight = Nothing Then
                        '-1
                        DistanceTerraHeight1 = -99999
                        ' DistanceTerraHeight = DistanceTerraHeight1.PadLeft(6)
                    ElseIf mUpMetData.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DistanceTerraHeight = -99999 Then
                        DistanceTerraHeight1 = -99999
                    Else
                        DistanceTerraHeight = mUpMetData.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DistanceTerraHeight + mUpMetData.title.TerraHeight  ' td6201 的每层位势高度（海拔）=离地高度+地面海拔.PadLeft(7)
                        DistanceTerraHeight = CInt(DistanceTerraHeight)
                        DistanceTerraHeight = "+" + DistanceTerraHeight.PadLeft(5, "0")
                    End If
                    '温度0.1摄氏度，4位，缺失写入-999
                    If mUpMetData.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DryTemperature = Nothing Then
                        '-0
                        DryTemperature1 = -999
                        ' DryTemperature = DryTemperature1.PadLeft(4)
                    ElseIf mUpMetData.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DryTemperature = "-9990" Then
                        mUpMetData.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DryTemperature = "-999"
                    Else
                        If mUpMetData.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DryTemperature.ToString.StartsWith("-") Then
                            DryTemperature = mUpMetData.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DryTemperature '.PadLeft(7)
                            DryTemperature = Mid(DryTemperature, 2, DryTemperature.Length - 1)
                            DryTemperature = DryTemperature.PadLeft(3, "0")
                            DryTemperature = "-" & DryTemperature
                        Else
                            DryTemperature = mUpMetData.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DryTemperature '.PadLeft(7)
                            DryTemperature = DryTemperature.PadLeft(3, "0")
                            DryTemperature = "+" & DryTemperature
                        End If

                    End If

                    '计算相对湿度，小阳改
                    Dim rh As Double = Dewpoint_RH(mUpMetData.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DryTemperature / 10, mUpMetData.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DewPointTemperature / 10)

                    sngRH = Format(rh, "0")

                    sngRH = sngRH.PadLeft(3, "0")
                    'If Me.m_reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DewPointTemperature = Nothing Then
                    '    '- - - 0
                    '    DewPointTemperature1 = 999
                    '    DewPointTemperature = DewPointTemperature1.PadLeft(3)
                    'Else
                    '    DewPointTemperature = Me.m_reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DewPointTemperature '.PadLeft(7)
                    '    DewPointTemperature = Mid(DewPointTemperature, 1, 3) ' DewPointTemperature.PadLeft(3)
                    'End If
                    '风向，单位度，缺失写入999	，3位
                    If mUpMetData.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).WindVane = Nothing Then
                        WindVane1 = 999
                    Else
                        WindVane = mUpMetData.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).WindVane
                        WindVane = WindVane.PadLeft(3, "0") 'Mid(WindVane, 1, 3) '
                    End If
                    '风速，单位m/s，缺失写入999	，3位
                    If mUpMetData.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).WindSpeed = Nothing Then
                        '-0
                        WindSpeed1 = 999
                    ElseIf mUpMetData.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).WindSpeed = 9990 Then
                        WindSpeed1 = 999

                    Else
                        '小阳改动
                        ' WindSpeed = Me.m_reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).WindSpeed
                        intwindspeed = CInt(mUpMetData.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).WindSpeed)
                        intwindspeed = intwindspeed / 10 ' * 10
                        WindSpeed = CSng(intwindspeed)
                        WindSpeed = WindSpeed.PadLeft(3, "0") 'Mid(WindSpeed, 1, 3)
                    End If


                    '写入每层数据信息
                    str = TIME_SINCE_RELEASE.PadLeft(5, "0") '气球经过时间，小阳改
                    str += pa '气压
                    str += DistanceTerraHeight '位势高度
                    str += DryTemperature '干球温度
                    str += sngRH ' "0" & sngRH '"070" ' 相对湿度，小阳改。
                    str += WindVane '风向
                    str += WindSpeed '风速
                    str += "000000" & "1" '每层的数据标志 calpuff 为“999999”+“9” aermod 为“000000”+“1”
                    swr.Write(str)
                Next
                swr.Write(vbCrLf)
            End If


        Next
        swr.Close()
        swr.Dispose()
    End Sub
    ''' <summary>
    ''' 干球温度、露点温度转为相对湿度，%
    ''' </summary>
    ''' <param name="Drytemp">干球温度</param>
    ''' <param name="Dewpoint">露点温度</param>
    ''' <returns>相对湿度 %</returns>
    ''' <remarks></remarks>
    Private Function Dewpoint_RH(ByVal Drytemp As Double, ByVal Dewpoint As Double) As Double
        Dim dblRH As Double
        Dim k As Double = 7.65 * Drytemp / (243.12 + Drytemp)

        Dim c As Double = (7.65 * Dewpoint - k * Dewpoint - 243.12 * k) / (243.12 - Dewpoint)

        dblRH = 10 ^ (c + 2)



        'Dim b1 As Double = (7.65 - k - 2) * Dewpoint + 243.12 * 2

        'Dim b2 As Double = b1 / (243.12 + Dewpoint)

        'dblRH = 10 ^ b2

        'Dim a1 As Double = 7.65 * Dewpoint
        'Dim a2 As Double = k * Dewpoint
        'Dim a3 As Double = 2 * Dewpoint
        'Dim a4 As Double = 2 * 243.12


        'Dim a5 As Double = a1 - a2 - a3 + a4

        'Dim a6 As Double = a5 / (243.12 + Dewpoint)

        'dblRH = 10 ^ a6
        Dewpoint_RH = CInt(dblRH)

    End Function
    ''' <summary>
    ''' 删除经纬度 中的小数点
    ''' </summary>
    ''' <param name="str"></param>
    ''' <remarks></remarks>
    Private Function CharIndex(ByVal str As String, ByVal i As Integer) As String
        Dim strIndex As Integer = -1
        strIndex = str.IndexOf(".")
        If strIndex <> -1 Then
            str = str.Remove(strIndex, 1)
        Else
            str = str
        End If
        If str.Length < i Then
            str = str.ToString.PadLeft(i, "0")
        ElseIf str.Length > i Then
            str = Mid(str, 1, i)
        Else
            str = str
        End If
        Return str
    End Function
End Class
